Skip to content

audio: ffmpeg_dec: add ALAC decoder and FLAC/ALAC encoder support - #11427

Draft
lgirdwood wants to merge 6 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-lossless
Draft

lgirdwood wants to merge 6 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-lossless

Conversation

@lgirdwood

Copy link
Copy Markdown
Member

Add Phase 1 lossless compress playback and capture codecs to ffmpeg_dec using native fixed-point libavcodec implementations:

  • ALAC decoder (CONFIG_FFMPEG_DEC_ALAC, SND_AUDIOCODEC_ALAC):
    • Apple .caf container parser (kuki chunk 36-byte ALAC magic cookie extraction and data chunk framing) and inline 36-byte QuickTime 'alac' atom header support.
    • Self-delimiting unparsed frame consumption tracking via GetBitContext and partial-frame reassembly across LL period boundaries.
  • FLAC encoder (CONFIG_FFMPEG_ENC_FLAC, SND_AUDIOCODEC_FLAC):
    • Native libavcodec flac encoder configured with fixed integer LPC predictors (FF_LPC_TYPE_FIXED, compression_level = 1) for deterministic low-MCPS DSP encoding.
    • Prepends the 42-byte "fLaC" + STREAMINFO metadata block from avctx->extradata to produce a self-contained .flac stream.
    • Bounds FlacEncodeContext dimensions on a build-tree copy of flacenc.c (FLAC_MAX_BLOCKSIZE=1152, rc_sums[1], FLAC_MAX_CHANNELS=2), reducing context allocation 122x from 7.35 MiB to 43.5 KiB.
  • ALAC encoder (CONFIG_FFMPEG_ENC_ALAC, SND_AUDIOCODEC_ALAC):
    • Native libavcodec alac encoder configured with fixed LPC prediction (compression_level = 1) and planar S16 input conversion.
    • Prepends the 36-byte QuickTime 'alac' magic cookie from avctx->extradata.
  • Topologies & Base FW Capabilities:
    • Advertise SND_AUDIOCODEC_ALAC playback and SND_AUDIOCODEC_FLAC / SND_AUDIOCODEC_ALAC capture in IPC4_SOF_CODEC_INFO.
    • Add sof-hda-generic-ffmpeg-alac-compr, sof-hda-generic-ffmpeg-flac-enc-compr, and sof-hda-generic-ffmpeg-alac-enc-compr development topology targets.

Hardware Verification (Aphid PTL ACE 3.0 & Spider TGL CAVS 2.5)

Verified end-to-end on both Aphid (ptl) and Spider (tgl):

  • ALAC .caf playback (comprC0D50, codec_id=15)
  • FLAC capture encode (comprC0D51, codec_id=10) + round-trip playback (comprC0D50)
  • ALAC capture encode (comprC0D51, codec_id=15) + round-trip playback (comprC0D50)

Advertise SND_AUDIOCODEC_ALAC decode and SND_AUDIOCODEC_FLAC /
SND_AUDIOCODEC_ALAC encode capabilities in base_fw when the
corresponding FFmpeg Kconfig options are enabled.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
@lgirdwood
lgirdwood force-pushed the topic/ffmpeg-lossless branch from f41fccf to 2a735e9 Compare November 10, 2026 11:51
Extend the FFmpeg decode and encode adapter infrastructure to support
container-framed and variable-blocksize lossless codecs:
- Add frame_bytes_consumed hook and unparsed packet reassembly buffer
  handling in ffmpeg_dec-ffmpeg.c for codecs without a standalone
  AVCodecParser (such as ALAC).
- Extend ffmpeg_dec_pcm_to_frame() in ffmpeg_dec-convert.c to support
  AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32, and
  AV_SAMPLE_FMT_S32P sample formats.
- Add an optional encoder flush() hook in struct ffmpeg_enc_codec_ops
  and support native libavcodec encoders in ffmpeg.cmake.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Add Apple Lossless Audio Codec (ALAC, SND_AUDIOCODEC_ALAC) decoder
support via libavcodec's integer alac decoder:
- Support raw ALAC packets with 36-byte out-of-band magic cookie
  extradata as well as self-contained Core Audio Format (.caf) streams.
- Resize the PCM output staging buffer if the magic cookie's
  max_samples per frame exceeds the initial allocation.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Add native libavcodec FLAC lossless encoder (SND_AUDIOCODEC_FLAC)
support for compress capture streams:
- Use fixed integer LPC predictors (FF_LPC_TYPE_FIXED,
  compression_level 1) with 1152-sample blocks and zero floating-point
  instructions in the per-frame encode path.
- Prepend the 42-byte "fLaC" + STREAMINFO header on stream start and
  convert input PCM via ffmpeg_dec_pcm_to_frame().

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Add native libavcodec ALAC lossless encoder (SND_AUDIOCODEC_ALAC)
support for 1..2 channel compress capture streams:
- Configure 1024-sample frame size using pure integer prediction and
  adaptive Rice coding.
- Prepend a 64-byte Core Audio Format (.caf) header ("caff" + "desc" +
  "kuki" + "data") so captured streams are directly decodable.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Add development topology targets for ALAC compress playback and
FLAC/ALAC compress capture on CAVS 2.5 and ACE 3.0.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
@lgirdwood
lgirdwood force-pushed the topic/ffmpeg-lossless branch from 2a735e9 to e3da217 Compare November 11, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants